package com.witmore.skyline.web.controller.skyladder;

import com.aliyun.swas_open20200601.models.ModifyFirewallRuleRequest;
import com.aliyun.tea.TeaException;
import com.witmore.skyline.web.controller.vo.result.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * Description: SkyLadderServiceImpl
 * Author: 豆子高
 * Date: 2025/4/17 10:02
 */
@Slf4j
@Service
public class SkyLadderServiceImpl implements SkyLadderService {
    @Override
    public ResponseResult<Void> skyLadderModify(ModifyFirewallRuleRequest rule, String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.swas_open20200601.Client client = this.createClient(accessKeyId, accessKeySecret);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            client.modifyFirewallRuleWithOptions(rule, runtime);
            return ResponseResult.success();
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            log.error("{}", error.getMessage());
            // 诊断地址
            log.error("{}", error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            log.error("{}", error.getMessage());
            // 诊断地址
            log.error("{}", error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return ResponseResult.fail(-1, "请求失败");
    }


    /**
     * <b>description</b> :
     * <p>使用AK&amp;SK初始化账号Client</p>
     *
     * @return Client
     * @throws Exception
     */
    public com.aliyun.swas_open20200601.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(System.getenv(accessKeyId))
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret(System.getenv(accessKeySecret));
        config.endpoint = "swas.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.swas_open20200601.Client(config);
    }

}
